<template>
  <div class="app-container">
    <div class="article-container">
      <h2 class="title" style="text-align: center">{{ formTitle }}</h2>
      <el-form label-width="120px">
        <!-- 文章标题 -->
        <el-form-item label="文章标题">
          <el-input v-model="articleInfo.title" placeholder=" 文章标题" />
        </el-form-item>

        <!-- 所属分类：级联下拉列表 -->
        <!-- 文章标签 -->
        <el-form-item label="文章标签">
          <el-select v-model="articleInfo.typeId" placeholder="标签">
            <el-option
              v-for="tag in tagList"
              :key="tag.id"
              :label="tag.title"
              :value="tag.id"
            />
          </el-select>
        </el-form-item>
        <!-- 文章内容-->
        <el-form-item label="文章内容">
          <!--        <tinymceEditor @writeContent="getWriteContent" v-show="isShowEdit" @saveDraft="saveDraft"-->
          <!--                       v-model="articleInfo.content"/>-->
          <tinymce-editor v-model="articleInfo.content"></tinymce-editor>
        </el-form-item>
        <!-- 课程封面 -->
        <el-form-item label="课程封面">
          <el-upload
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :action="BASE_API + '/eduoss/fileoss'"
            class="avatar-uploader"
          >
            <img
              class="avatar"
              v-show="articleInfo.cover"
              :src="articleInfo.cover"
            />
          </el-upload>
        </el-form-item>

        <el-form-item>
          <el-button
            :disabled="saveBtnDisabled"
            type="primary"
            @click="saveOrUpdate"
            >提交文章</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import articleApi from "@/api/article";
import TinymceEditor from "@/components/tinymce-editor/tinymce-editor.vue";
export default {
  components: {
    TinymceEditor,
  },
  //params.id获取路径id值
  asyncData({ params, error }) {
    return {
      memberId: params.id,
    };
  },
  data() {
    return {
      content: "## 这里是要展示的markdown文字，也可以通过props传递",
      articleInfo: {
        //封装文章数据
        authorId: "", //作者
        typeId: "", //标签
        title: "", //标题
        cover: "", //封面
        content: "", //文章内容
      },
      BASE_API: process.env.baseUrl, // 接口API地址
      tagList: [], // 分类列表
      saveBtnDisabled: false, // 保存按钮是否禁用
      articleId: "",
      formTitle: "添加文章",
    };
  },
  created() {
    //从router中获取要更新的文章ID
    this.articleId = this.$route.query.articleId;
    this.articleInfo.authorId = this.memberId
    //初始化标签
    this.initAllTag();
    //初始化封面
    this.articleInfo.cover =
      "https://edu-guli-0313.oss-cn-beijing.aliyuncs.com/2021/03/05/d1d36db7eb4045009edaead44224cdf7u=1572376661,3890953672&fm=26&gp=0.jpg";
    //根据router中有无文章ID判断是否更新文章
    if (this.articleId != null) {
      this.formTitle = "更新文章";
      this.initArticle();
    }
  },
  methods: {
    saveOrUpdate() {
      //如果文章ID不为空则更新文章
      if (this.articleId != null) {
        console.log(this.articleId);
        this.updateArticle();
      } else {
        //否则添加文章
        this.saveArticle();
      }
    },
    //查询所有标签
    initAllTag() {
      articleApi.getAllTag().then((response) => {
        this.tagList = response.data.data.list;
      });
    },
    //初始化文章
    initArticle() {
      articleApi.getArticleInfo(this.articleId).then((response) => {
        this.articleInfo = response.data.data.article;
      });
    },
    //上传之前调用的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //上传封面成功调用的方法
    handleAvatarSuccess(res, file) {
      this.articleInfo.cover = res.data.url;
    },
    //更新文章
    updateArticle() {
      console.log("更新文章");
      articleApi
        .updateArticleInfo(this.articleInfo)
        .then((response) => {
          if (response.data.success) {
            this.$message.success("提交成功");
            //跳转我的文章页面
            this.$router.push("/article/myArticle/" + this.memberId);
          }
        })
        .catch((err) => {
          this.$message.error("提交失败");
        });
    },
    //添加文章
    saveArticle() {
      articleApi
        .addArticle(this.articleInfo)
        .then((response) => {
          if (response.data.success) {
            this.$message.success("提交成功");
            //跳转我的文章页面
            this.$router.push("/article/myArticle/" + this.memberId);
          }
        })
        .catch((err) => {
          this.$message.error("提交失败");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.app-container {
  display: flex;
  justify-content: center;
}

.title {
  color: #4f4c4c;
  margin: 20px 0;
}
</style>
